/*Copyright (C) 2009 - 2011 MASSART GAUTHIER

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/

#include "../headers.h"


void pui_affichage(short matrice[43], const char *message, const char *nom_joueur, short colonne, short joueur) {
	short y = 6, x = x_begin_center(77), i = 0, j = 0, jeton = 1;

	if(colonne != 0 && joueur != 0) { // Si il y a une valeur pour colonne, on test quelle case occupera le jeton
		for(i = 5; i != -1; i--) {
			if(matrice[colonne + (i*7)] == ' ') {
				matrice[colonne + (i*7)] = '3';
				i = 0;
			}
		}
	}

	CSLP_Color(COLOR_BLUE, COLOR_BLACK);
	for(i = 0; i != 6; i++) {
		for(j = 0; j != 7; j++) {
			if(i == 0) {
				if(j + 1 == colonne) {
					if(joueur == 1) {
						CSLP_Color(COLOR_GREEN, COLOR_YELLOW);
					}
					else if(joueur == 2) {
						CSLP_Color(COLOR_GREEN, COLOR_RED);
					}
					mvaddstr(y - 1, x, "█▄▄▄▄▄▄▄▄▄█");
					CSLP_Color(COLOR_BLUE, COLOR_BLACK);
				}
				else {
					mvaddstr(y - 1, x, "█▄▄▄▄▄▄▄▄▄█");
				}
			}

			if(matrice[jeton] == '1') {
				CSLP_ColorBG(COLOR_YELLOW);
			}
			else if(matrice[jeton] == '2') {
				CSLP_ColorBG(COLOR_RED);
			}
			else if(matrice[jeton] == '3') {
				CSLP_ColorBG(COLOR_GREEN);
			}
			else {
				CSLP_ColorBG(COLOR_BLACK);
			}
			mvaddstr(y, x,     "███████████");
			mvaddstr(y + 1, x, "███▀   ▀███");
			mvaddstr(y + 2, x, "██ ");
			if(matrice[jeton] == '3') {
				if(matrice[jeton] == '3') {
					if(joueur == 1) {
						CSLP_ColorText(COLOR_YELLOW);
					}
					else if(joueur == 2) {
						CSLP_ColorText(COLOR_RED);
					}
					else {
						CSLP_ColorText(COLOR_GREEN);
					}
				}
				addstr("▄███▄");
				CSLP_ColorText(COLOR_BLUE);
			}
			else {
				addstr("     ");
			}
			addstr(" ██");
			mvaddstr(y + 3, x, "██ ");
			if(matrice[jeton] == '3' || matrice[jeton] == '4' || matrice[jeton] == '5') {
				if(matrice[jeton] == '3') {
					if(joueur == 1) {
						CSLP_ColorText(COLOR_YELLOW);
					}
					else if(joueur == 2) {
						CSLP_ColorText(COLOR_RED);
					}
					else {
						CSLP_ColorText(COLOR_GREEN);
					}
				}
				else if(matrice[jeton] == '4') {
					CSLP_ColorText(COLOR_YELLOW);
				}
				else if(matrice[jeton] == '5') {
					CSLP_ColorText(COLOR_RED);
				}
				addstr("▀███▀");
				CSLP_ColorText(COLOR_BLUE);
			}
			else {
				addstr("     ");
			}
			addstr(" ██");
			mvaddstr(y + 4, x, "███▄   ▄███");
			mvaddstr(y + 5, x, "███████████");
			CSLP_ColorBG(COLOR_BLACK);
			x += 11;
			jeton++;
		}
		y += 5;
		x = x_begin_center(77);
	}

	for(i = 1; i != 43; i++) { // On retire tous les jetons de test de la matrice
		if(matrice[i] == '3') {
			matrice[i] = ' ';
		}
	}

	if(colonne == 0 && joueur != 0) {
		CSLP_Color(COLOR_WHITE, COLOR_CYAN);
	}
	else {
		CSLP_ColorText(COLOR_WHITE);
	}

	mvaddstr(34, 5, "┌─────────┐");
	mvaddstr(35, 5, "│ QUITTER │");
	mvaddstr(36, 5, "└─────────┘");
	CSLP_Color(COLOR_BLUE, COLOR_BLACK);

	pui_afficher_message(message, nom_joueur);

	refresh();
}

void pui_afficher_message(const char *message, const char *nom_joueur) {
	save_cursor_pos();
	move(38, 0);
	CSLP_Color(COLOR_RED, COLOR_BLACK);
	clear_line();
	afficher_centre(nom_joueur);
	CSLP_ColorText(CT);
	clear_line();
	afficher_centre(message);
	restore_cursor_pos();
	CSLP_ColorText(COLOR_WHITE);
}
